package com.macro.mall.controller;

import com.macro.mall.annotationUtil.CurrentUserName;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.*;
import com.macro.mall.mapper.UmsStoreMapper;
import com.macro.mall.model.OmsOrder;
import com.macro.mall.model.UmsStore;
import com.macro.mall.model.UmsStoreExample;
import com.macro.mall.service.OmsOrderService;
import com.macro.mall.service.UmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单管理Controller
 */
@Controller
@Api(tags = "OmsOrderController", description = "订单管理")
@RequestMapping("/order")
public class OmsOrderController {
    @Autowired
    private OmsOrderService orderService;
    @Autowired
    private UmsAdminService umsAdminService;
    @Autowired
    private UmsStoreMapper umsStoreMapper;


    @ApiOperation("查询订单")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<OmsOrder>> list(@CurrentUserName String currentUserName,
                                                    @RequestBody OmsOrderQueryParam queryParam) {
        UmsAdminDTO umsAdminDTO = umsAdminService.getAdminDTOByUserName(currentUserName);
        if(umsAdminDTO.getAdminType().equals(2)){
            queryParam.setStoreId(umsAdminDTO.getStoreId());
        }else if(umsAdminDTO.getAdminType().equals(1)){
            UmsStoreExample storeExample = new UmsStoreExample();
            storeExample.createCriteria().andParentIdEqualTo(umsAdminDTO.getId());
            List<Long> stoIds = this.umsStoreMapper.selectByExample(storeExample).stream().map(UmsStore::getId).collect(Collectors.toList());
            queryParam.setStoreIds(stoIds);
        }
        List<OmsOrder> orderList = orderService.list(queryParam, queryParam.getPageSize(), queryParam.getPageNum());
        return CommonResult.success(CommonPage.restPage(orderList));
    }

    @ApiOperation("批量发货")
    @RequestMapping(value = "/update/delivery", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delivery(@RequestBody List<OmsOrderDeliveryParam> deliveryParamList) {
        int count = this.orderService.delivery(deliveryParamList);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 单个发货获取发货商品信息及库存信息
     * @author zhouboyu
     * @Date 2020年9月12日
     * @param orderId
     * @param agentId
     * @return
     */
    @ApiOperation("单个发货发货获取发货商品信息及库存信息")
    @RequestMapping(value = "/delivery/data", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<List<OmsOrderItemDTO>> deliveryData(@RequestBody DeliveryOneDataParam deliveryOneDataParam) {
        return this.orderService.deliveryData(deliveryOneDataParam.getOrderId(),deliveryOneDataParam.getAgentId());
    }
    /**
     * 单个订单发货
     * @author zhouboyu
     * @Date 2020年9月11日
     * @param deliveryParam
     * @return
     */
    @ApiOperation("单个发货发货")
    @RequestMapping(value = "/update/deliveryOne", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deliveryOne(@RequestBody OmsOrderDeliveryParam deliveryParam) {
        int count = 0;
        try {
            count = orderService.deliveryOne(deliveryParam);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed(e.getMessage());
        }
       return CommonResult.success(count);
    }
    @ApiOperation("批量关闭订单")
    @RequestMapping(value = "/update/close", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult close(@RequestBody FilterParam param) {
        int count = orderService.close(param.getIds(), param.getNote());
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("批量删除订单")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = orderService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("获取订单详情:订单信息、商品信息、操作记录")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<OmsOrderDetail> detail(@PathVariable Long id) {
        OmsOrderDetail orderDetailResult = orderService.detail(id);
        return CommonResult.success(orderDetailResult);
    }

    @ApiOperation("修改收货人信息")
    @RequestMapping(value = "/update/receiverInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateReceiverInfo(@RequestBody OmsReceiverInfoParam receiverInfoParam) {
        int count = orderService.updateReceiverInfo(receiverInfoParam);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("修改订单费用信息")
    @RequestMapping(value = "/update/moneyInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateReceiverInfo(@RequestBody OmsMoneyInfoParam moneyInfoParam) {
        int count = orderService.updateMoneyInfo(moneyInfoParam);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("备注订单")
    @RequestMapping(value = "/update/note", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateNote(@RequestBody OmsOrder order){
//        @RequestParam("id") Long id,
//        @RequestParam("note") String note,
//        @RequestParam("status") Integer status){
        int count = orderService.updateNote(order.getId(), order.getNote(), order.getStatus());
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("把一个订单归到某个经纪人下")
    @RequestMapping(value = "/giveOrderToAgent", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult giveOrderToAgent(@RequestParam Long orderId, @RequestParam Long agentId){
        return orderService.giveOrderToAgent(orderId, agentId);
    }
}